<template>
	<!-- 二维码弹出层 -->
	<view>
		<u-popup :show="showFlag" @close="close" @open="open" mode="center" round="20" :safeAreaInsetBottom="false">
			<view class="popup">
				<image :src="imgUrl" class="popup-code" mode=""></image>
				<view class="popup-btn" @click="preservation">
					保存到本地
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		name: "CodePopup",
		props: {
			imgUrl: {
				type: String,
				require: true
			},
			// 显示弹出层
			showFlag: {
				default: false,
				type: Boolean
			}
		},
		data() {
			return {};
		},
		methods: {
			// 弹出层打开
			open() {
				// console.log('open');
			},
			// 弹出层关闭
			close() {
				this.$emit('popupClose')
			},
			// 保存按钮
			preservation() {
				let that = this
				uni.showLoading({
					mask: true,
					title: "图片保存中"
				})
				uni.downloadFile({
					url: this.imgUrl, //图片地址
					success: (res) => {
						if (res.statusCode === 200) {
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: function() {
									uni.hideLoading()
									uni.showToast({
										title: "保存成功！"
									})
									that.$emit('popupBtnCallback')
								},
								fail() {
									uni.hideLoading()
									uni.showToast({
										title: '保存失败',
										icon: 'error',
										duration: 2000
									})
								}
							});
						}
					},
					fail: (err) => {
						uni.hideLoading()
						uni.showToast({
							title: '图片地址错误',
							con: 'error',
							duration: 2000
						})
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.popup {
		width: 600rpx;
		box-sizing: border-box;
		padding: 64rpx 0 48rpx;
		display: flex;
		justify-content: center;
		flex-wrap: wrap;

		&-code {
			width: 500rpx;
			height: 500rpx;
			border-radius: 24rpx;
			border: 2rpx solid rgba(255, 255, 255, 0.22);
		}

		&-btn {
			margin-top: 64rpx;
			width: 494rpx;
			height: 88rpx;
			border-radius: 600rpx;
			opacity: 1;
			background: rgba(128, 110, 254, 1);
			color: rgba(255, 255, 255, 1);
			font-size: 32rpx;
			font-weight: 500;
			text-align: center;
			line-height: 88rpx;
			letter-spacing: 0.6rpx;
		}
	}
</style>
